<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 直接内存深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            padding: 16px 20px;
            border-radius: 8px;
            font-family: 'Consolas', 'Monaco', monospace;
            position: relative;
            overflow-x: auto;
        }
        
        .code-block::before {
            content: 'JAVA';
            position: absolute;
            top: 8px;
            right: 12px;
            font-size: 12px;
            color: #666;
            font-weight: 600;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                Java 直接内存
                <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度解析与实践指南</span>
            </h1>
            <p class="text-xl md:text-2xl font-light max-w-3xl mx-auto leading-relaxed opacity-95">
                探索 Java 中的高级内存管理技术，掌握直接内存的原理、优势与最佳实践
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-memory text-3xl mb-2"></i>
                    <p class="text-sm opacity-90">高效内存管理</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-rocket text-3xl mb-2"></i>
                    <p class="text-sm opacity-90">性能优化</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-shield-alt text-3xl mb-2"></i>
                    <p class="text-sm opacity-90">稳定可靠</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="prose prose-lg max-w-none">
                <p class="text-lg leading-relaxed text-gray-700 first-letter:text-6xl first-letter:font-bold first-letter:text-gradient first-letter:float-left first-letter:mr-3">
                    <strong>直接内存</strong>是 Java 中一个相对高级的内存管理概念，它与 JVM 的堆内存不同，具有特定的用途和特性。它通过操作系统的 API 直接分配和管理内存，具有高效、减少 GC 压力的优点，但也带来了管理复杂性和内存碎片的问题。
                </p>
            </div>
        </section>

        <!-- What is Direct Memory -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 section-title">什么是直接内存？</h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                直接内存是指不在 Java 堆内存（Heap）中分配的内存区域。它是由 JVM 直接通过操作系统的原生 API 分配的，通常用于处理高性能 I/O 操作和大型数据缓存。
            </p>
            <div class="code-block">
                ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024);
            </div>
            <p class="text-gray-600 mt-4">
                直接内存的分配和释放由 Java 程序员通过 NIO（New I/O）库中的 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">ByteBuffer</code> 类来管理。
            </p>
        </section>

        <!-- Memory Architecture Visualization -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">内存架构可视化</h2>
            <div class="mermaid">
                graph TB
                    subgraph JVM["JVM 内存空间"]
                        Heap["堆内存<br/>• 对象实例<br/>• 数组"]
                        Stack["栈内存<br/>• 局部变量<br/>• 方法调用"]
                        Method["方法区<br/>• 类信息<br/>• 常量池"]
                    end
                    
                    subgraph OS["操作系统内存"]
                        Direct["直接内存<br/>• NIO Buffer<br/>• 高性能 I/O"]
                        Native["本地内存<br/>• JNI 调用<br/>• 系统资源"]
                    end
                    
                    JVM -.->|"allocateDirect()"| Direct
                    Direct -.->|"系统调用"| Native
                    
                    style Heap fill:#ffd93d,stroke:#333,stroke-width:2px
                    style Direct fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                    style JVM fill:#f0f0f0,stroke:#333,stroke-width:2px
                    style OS fill:#e0e0e0,stroke:#333,stroke-width:2px
            </div>
        </section>

        <!-- Implementation Details -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <h2 class="text-3xl font-bold mb-6 section-title">直接内存的实现</h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-database"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">不在 Java 堆中</h3>
                    <p class="text-gray-700">与堆内存不同，直接内存不受 JVM 堆的大小限制。它是直接从操作系统的物理内存中分配的。</p>
                </div>
                <div class="bg-gradient-to-br from-purple-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-cogs"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">通过操作系统 API 分配</h3>
                    <p class="text-gray-700">直接内存的分配是通过操作系统提供的 API（如 <code class="bg-white px-2 py-1 rounded">malloc</code>）进行的，而不是由 JVM 自己管理的。</p>
                </div>
                <div class="bg-gradient-to-br from-purple-50 to-indigo-50 p-6 rounded-xl">
                    <div class="feature-icon">
                        <i class="fas fa-code"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">使用 NIO ByteBuffer</h3>
                    <p class="text-gray-700">在 Java 中，直接内存通常通过 <code class="bg-white px-2 py-1 rounded">java.nio.ByteBuffer</code> 的 <code class="bg-white px-2 py-1 rounded">allocateDirect()</code> 方法来申请。</p>
                </div>
            </div>
        </section>

        <!-- Advantages and Disadvantages -->
        <section class="grid md:grid-cols-2 gap-8 mb-8">
            <!-- Advantages -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 text-green-600">
                    <i class="fas fa-check-circle mr-3"></i>优点
                </h2>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-bolt text-green-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-lg mb-1">高效的 I/O 操作</h4>
                            <p class="text-gray-700">减少了数据在 Java 堆内存和操作系统内存之间的复制操作，从而减少了内存复制的开销。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-recycle text-green-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-lg mb-1">减少 GC 压力</h4>
                            <p class="text-gray-700">直接内存不在 JVM 堆内存中，因此不会增加垃圾回收（GC）的负担。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-microchip text-green-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-lg mb-1">更接近底层</h4>
                            <p class="text-gray-700">允许 Java 程序员更直接地与操作系统的内存进行交互，这对于某些高性能应用非常重要。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Disadvantages -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 text-red-600">
                    <i class="fas fa-exclamation-triangle mr-3"></i>缺点
                </h2>
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-puzzle-piece text-red-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-lg mb-1">管理复杂性</h4>
                            <p class="text-gray-700">直接内存的分配和释放需要程序员显式管理，增加了编程复杂性。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-lock text-red-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold text-lg mb-1">受限于操作系统</h4>
                            <p class="text-gray-700">直接内存的大小受限于操作系统的物理内存，而不是 JVM 堆的限制。</p>
                        </div>